#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 5e5 + 10, INF = 0x3f3f3f3f;

int n, m;
int a[N], pre[N], q[N];
ll f[N];

void solve()
{
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    a[++n] = 0;
    cin >> m;
    for (int i = 0;i <= n;i++) pre[i] = 0;
    for (int i = 1;i <= m;i++) {
        int l, r;cin >> l >> r;
        pre[r + 1] = max(pre[r + 1], l);
    }
    for (int i = 1;i <= n;i++) pre[i] = max(pre[i], pre[i - 1]);

    int l = 0, r = -1;
    q[++r] = 0;
    for (int i = 1;i <= n;i++) {
        while (l <= r && q[l] < pre[i]) l++;
        f[i] = f[q[l]] + a[i];
        while (l <= r && f[q[r]] >= f[i])r--;
        q[++r] = i;
    }
    cout << f[n] << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}